import axios from "axios";
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import store from "@/store";

const request  = axios.create({
    baseURL:"/api1/api",
    timeout:2000
})

request.interceptors.request.use(
    (config)=>{
        NProgress.start()
        const userTempId = store.state.user.userTempId
        config.headers.userTempId = userTempId

        const token = store.state.user.token
        config.headers.token = token

        return config
    },
    (error)=>{
        return Promise.reject(error)
    }
)

request.interceptors.response.use(
    (response)=>{
        NProgress.done()
        if(response.data.code === 200){
            return response.data.data
        }
        return Promise.reject({message:response.data.message})
    },
    (error)=>{
        NProgress.done()
        return Promise.reject(error)
    }
)

export default request